package com.mp3player.util;

import android.net.Uri;

import com.mp3player.model.Mp3Info;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cuckoo322 on 3/19/2017.
 */

public class FileUtils {

    public static final String SEPERATOR = "/";

    private String InternalStorageRoot;

    public FileUtils(String root) {
        InternalStorageRoot = root;
    }

    public FileUtils(File file) {
        InternalStorageRoot = file.getAbsolutePath();
    }

    /**
     * 创建文件
     * @param dir 创建文件所在的路径
     * @param fileName 创建的文件名
     * @return 返回被创建的文件
     * @throws IOException 文件创建过程中的IO异常
     */
    public File createFile(String dir, String fileName) throws IOException {
        File dir_ = new File(InternalStorageRoot+"/"+dir);
        if (!dir_.exists()) {
            dir_.mkdirs();
        }
        File file = new File(InternalStorageRoot+"/"+dir+fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    public File createDir(String dir) {
        File dir_ = new File(InternalStorageRoot+dir);
        if (!dir_.exists()) {
            dir_.mkdirs();
        }
        return dir_;
    }

    /**
     * 将输入流写入到指定的文件路径
     * @param path 指定路径
     * @param fileName 指定文件名
     * @param inputStream 输入流
     * @return 输入流被写入到的文件的File类
     */
    public File writeFromInput(String path, String fileName, InputStream inputStream) {
        File file = null;
        OutputStream outputStream = null;
        try {
            this.createDir(path);
            file = createFile(path, fileName);
            outputStream = new FileOutputStream(file);
            byte buffer[] = new byte[4 * 1024];
            int temp;
            while ((temp = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, temp);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public boolean isFileExist(String fileName, String dir) {
        return new File(InternalStorageRoot+SEPERATOR+dir+fileName).exists();
    }

    public static void recursiveRemove(File file) throws Exception {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files.length < 1) {
                file.delete();
            }
            for (File f : files) {
                recursiveRemove(f);
                f.delete();
            }
        } else if (file.isFile()) {
            file.delete();
        } else {
            throw new Exception("不支持这种不是目录又不是文件的东西的删除");
        }
    }

    /**
     * 扫描path，获得mp3文件信息列表
     * @param path 需要扫描的路径
     * @return 该path下的mp3文件列表
     */
    public static List<Mp3Info> getMp3Files(String path) {
        List<Mp3Info> mp3InfoList = new ArrayList<>();
        File absoluteSaveDir = new File(path);
        if (absoluteSaveDir.exists()) {
            File[] files = absoluteSaveDir.listFiles();
            for (File f : files) {
                if (f.getName().endsWith("mp3")) {
                    Mp3Info mp3Info = new Mp3Info();
                    mp3Info.setMp3Name(f.getName());
                    mp3Info.setMp3Size(f.length() + "");
                    mp3Info.setLrcName(f.getName().substring(0, f.getName().indexOf("."))+".lrc");
                    mp3InfoList.add(mp3Info);
                }
            }
        }
        return  mp3InfoList;
    }

    /**
     * 获得mp3文件的资源uri
     * @param mp3Info mp3信息实例
     * @return mp3文件的资源uri
     */
    public static Uri getMp3Uri(Mp3Info mp3Info) {
        String mp3Path = MetaData.root.getAbsolutePath()
                +FileUtils.SEPERATOR
                +MetaData.SAVE_DIR
                +mp3Info.getMp3Name();
        Uri uri = Uri.parse("file://"+mp3Path);
        return uri;
    }
}
